#include <bits/stdc++.h>

#define endl "\n"
#define int long long

using namespace std;

int a[200010];
int b[200010];
int c[200010];
vector<int> e1, e2;
vector<int> t[200010];

signed main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int T; cin >> T;
    while (T -- )
    {
        int n; cin >> n;
        for (int i = 1; i <= n; i ++ ) cin >> a[i];
        if (n <= 15)
        {
            int res = 0;
            for (int i = 0; i < (1 << n); i ++ )
            {
                int tot1 = 0, tot2 = 0, ans = 0;
                for (int j = 0; j < n; j ++ )
                    if (i >> j & 1) b[ ++ tot1] = a[j + 1];
                    else c[ ++ tot2] = a[j + 1];
                for (int i = 1; i <= tot2; i ++ )
                    ans += (c[i] == c[i - 1]) * c[i - 1];
                for (int i = 1; i <= tot1; i ++ )
                    ans += (b[i] == b[i - 1]) * b[i - 1];
                res = max(res, ans);
            }
            cout << res << endl;
            continue;
        }
        for (int i = 1; i <= 200000; i ++ )
            t[a[i]].push_back(i);
        int res = 0;
        for (int i = 0; i < (1 << 10); i ++ )
        {
            e1.clear(); e2.clear();
            for (int j = 0; j < 10; j ++ )
                if (i >> j & 1)
                    for (int x : t[j + 1])
                        e1.push_back(x);
                else
                    for (int x : t[j + 1])
                        e2.push_back(x);
            sort(e1.begin(), e1.end());
            sort(e2.begin(), e2.end());
            int ans = 0;
            for (int i = 1; i < e1.size(); i ++ )
                ans += (a[e1[i]] == a[e1[i - 1]]) * a[e1[i]];
            for (int i = 1; i < e2.size(); i ++ )
                ans += (a[e2[i]] == a[e2[i - 1]]) * a[e2[i]];
            res = max(res, ans);
        }
        cout << res << endl;
    }

    return 0;
}
